import { AppModel } from '../../../../model/AppModel'

export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.operators.primary,
        Blockly.Colours.operators.secondary,
        Blockly.Colours.operators.tertiary
    ] ,

    previousStatement: false ,
    nextStatement: false ,
    hat: false ,

    init(model:AppModel) {
        this.appendDummyInput()
            .appendField("颜色值")
        this.appendValueInput("color")

        Blockly.Extensions.apply("output_string", this)
    } ,

    run (args: any, util: any, runtime: any) {
        return args.color
    } ,

    coder: {
        JavaScript(block: any, coder: any) {
            if(!block.childBlocks_.length) 
                return [`[0,0,0]`, coder.ORDER_ATOMIC]
            let field = block.childBlocks_[0].getField("COLOUR")
            let color = field.getValue()
            if(typeof(color)!='string' || color.length!=7) {
                return [`[0,0,0]`, coder.ORDER_ATOMIC]
            }
            let red = parseInt(color.substr(1,2), 16)
            let green = parseInt(color.substr(3,2), 16)
            let blue = parseInt(color.substr(5,2), 16)
                
            return [`[${red},${green},${blue}]`, coder.ORDER_ATOMIC]
        }
    }
}
